package com.jack.thinking.thread.core._3;

public class CrossValidation {
	public static void main(String[] args) {
		DBTools dt = new DBTools();
		Thread t1 = new Thread() {
			@Override
			public void run() {
				for (int i = 0; i < 5; i++) {
					dt.backUpA();
				}
			}
		};
		t1.start();
		
		Thread t2 = new Thread() {
			@Override
			public void run() {
				for (int i = 0; i < 5; i++) {
					dt.backUpB();
				}
			}
		};
		t2.start();
	}
}

class DBTools{
	private boolean flag = true;
	
	synchronized public void backUpA() {
		try {
			while(flag == true) {
				wait();
			}
			System.out.println("*****");
			flag = true;
			notify();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	synchronized public void backUpB() {
		try {
			while(flag == false) {
				wait();
			}
			System.out.println("-----");
			flag = false;
			notify();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
